home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7808 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How big is the 0
  5. Date: 19 Feb 1996 20:05:10 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4g9eg6$pks@werple.net.au>
  8. References: <1996Feb17.132420.114207@kuhub.cc.ukans.edu>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. anh@kuhub.cc.ukans.edu writes:
  12.  
  13.  
  14. >Hello,
  15.  
  16. >In C, anytime I need to do a null pointer check I would do this:
  17.  
  18. >    MyType *p;
  19.  
  20. >    if(p==(MyType*)0)
  21. >        bla;
  22.  
  23. >The new operator supposedly return a 0 upon failure. Can I rely on the 
  24. >compiler to make sure the 0 is as big as the size of the pointer? For 
  25. >example, ALPHA's pointers are 64 bits long which is different from the 32 bits
  26. >int.
  27.  
  28. The 0 will be the same size as the pointer size. This 0 has nothing to do
  29. with the number 0. It isn't even guaranteed to contain all zero bits. A
  30. compiler, if it wishes, can use different bit patterns for different
  31. types, e.g., (char*)0 and (int*)0 can't be guaranteed to contain the same
  32. bit pattern, but it doesn't matter because the compiler would choose the 
  33. 'right' value for the type anywhere you test or assign a pointer to 0.
  34. Null pointers are fundamental to the language; very few programs would 
  35. run correctly if the compiler did not implement them correctly.
  36.  
  37. BTW, I believe, according to the latest draft standard, that 'new' throws
  38. an exception if it fails, rather than returning 0. Also, you don't need to
  39. cast the 0; in the code above, 'if(p == 0)' or 'if(!p)' is the usual
  40. practice. 
  41.  
  42. David White
  43. davidw@werple.mira.net.au
  44.